package algorithms.que13;

/**
 * @功能
 * @创建时间 2020/8/1
 */
public class MoveToFront<Item> {
    Node first;
    public void add(Item item){
        del(item);
        first=first.add(item);
    }
    public void del(Item item){
        Node tmp = first;
        Node pp = new Node();
        if(!tmp.hasNext()&&tmp.item==item){
            first=null;
        }else {
        while (tmp.hasNext())
        {  if(tmp.next.item==item){
            pp=tmp.next;

            tmp.next=tmp.next.next;
            pp.item=null;
            break;
            }
            tmp=tmp.next;
        }}

    }
    class Node<item>{
        Node next;
        Item item;
        public boolean hasNext(){
            return next!=null;
        }
        public Node add(Item item){
            Node tmp = new Node();
            tmp.item=this.item;
            tmp.next=this.next;
            this.item=item;
            this.next=tmp;
          return this;
        }

    }

}
